<?php
!defined ('P_W') && exit('Forbidden');
require_once R_P.'mode/house/lib/db/BaseDB.php';
class PW_PricedetailDB extends PW_BaseDB{
	var $_tablename = "pw_house_pricedetail";
	
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 根据楼盘ID获得楼盘价格明细
	 * @param $hid	楼盘ID
	 * @return unknown_type
	 */
	function queryPricedetailByHid($hid,$page=1){
		$result = array();
		if ($hid) {
			$_sql = "SELECT count(*) FROM ".$this->_tablename." WHERE hid=".pwEscape($hid);
			$count = $this->_db->get_value($_sql);
			list($result ['pages'],$_limit) = $this->_getPages((int)$count,$page);
			$_sql = "SELECT * FROM ".$this->_tablename." WHERE hid=".pwEscape($hid)." ORDER BY id DESC $_limit";
			$query = $this->_db->query($_sql);
			while ($rt = $this->_db->fetch_array($query)) {
				$rt['opentime'] = $rt['opentime'] ? get_date($rt['opentime'], 'Y-m-d') : '';
				$rt['paytime'] = $rt['paytime'] ? get_date($rt['paytime'], 'Y-m-d') : '';
				$rt['currentprice'] = $rt['currentprice'] ? $rt['currentprice'] : '';
				$result['contents'][] = $rt;
			}
		}
		return $result;
	}
	
	function getPriceDetail($hid,$num) {
		$result = array();
		$hid = (int) $hid;
		if (!$hid) return array();
		$_sql = "SELECT * FROM ".$this->_tablename." WHERE hid=".pwEscape($hid)." ORDER BY id DESC ".pwLimit(0,$num);
		$query = $this->_db->query($_sql);
		while ($rt = $this->_db->fetch_array($query)) {
			$rt['opentime'] = $rt['opentime'] ? get_date($rt['opentime'], 'Y-m-d') : '';
			$rt['paytime'] = $rt['paytime'] ? get_date($rt['paytime'], 'Y-m-d') : '';
			$rt['currentprice'] = $rt['currentprice'] ? $rt['currentprice'] : '';
			$result[] = $rt;
		}
		return $result;
	}
	
	function queryPricedetailById($id){
		$result = array();
		if ($id) {
			$_sql = "SELECT * FROM ".$this->_tablename." WHERE id=".pwEscape($id);
			$result = $this->_db->get_one($_sql);
			$result['opentime'] && $result['opentime'] = $result['opentime'] ? get_date($result['opentime'], 'Y-m-d') : '';
			$result['paytime'] && $result['paytime'] = $result['paytime'] ? get_date($result['paytime'], 'Y-m-d') : '';
			$result['currentprice'] = $result['currentprice'] ? $result['currentprice'] : '';
		}
		return $result;
	}

	function getPricedetailByIds($hids) {
		if (!$hids) {
			return array();
		}
		$array = array();
		$query = $this->_db->query("SELECT hid,title,currentprice FROM " . $this->_tablename . " WHERE hid IN(" . pwImplode($hids) . ")");
		while ($rt = $this->_db->fetch_array($query)) {
			$array[$rt['hid']][] = $rt;
		}
		return $array;
	}
	
	/**
	 * 更新楼盘Id更新楼盘价格明细
	 * @param $hid
	 * @return unknown_type
	 */
	function updatePricedetailByHid($data,$hid){
		if (is_array($data) && !empty($data) && $hid) {
			$_sql = "UPDATE ".$this->_tablename." SET " . pwSqlSingle($data);
			$this->_db->update($_sql);
		}
	}
	
	/**
	 * 插入楼盘价格明细
	 * @param $data
	 * @return unknown_type
	 */
	function replacePricedetail($data,$hid) {
		if (is_array($data) && !empty($data)) {
			$_sql = "REPLACE INTO ".$this->_tablename." SET ".pwSqlSingle($data);
			$this->_db->update($_sql);
		}
	}
	
	/**
	 * 根据ID删除楼盘价格明细
	 * @param $id
	 * @return unknown_type
	 */
	function deletePricedetail($id){
		if ($id) {
			(!is_array($id) && is_numeric($id)) && $id = array($id);
			$_sql = "DELETE FROM ".$this->_tablename." WHERE id IN (".pwImplode($id).")";
			$this->_db->update($_sql);
		}
	}
	
}
?>